<?php
namespace app\api\controller;

use app\api\model\Cardnfc as Cfc;

class Cardnfc extends Controller
{
    //NFC卡

    //实例化
    public function obj(){
        return new Cfc();
    }

    //商户登录
    public function dologin()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
//        return $this->toJson(0, '', shopdod_hash('123456'));
        $uname = isset($param['uname']) ? $param['uname'] : '';
        $passwd = isset($param['passwd']) ? $param['passwd'] : '';
        if (!$uname) {
            return $this->toJson(-2, '账户必须的');
        }
        if (!$passwd) {
            return $this->toJson(-2, '密码必须的');
        }
        if (strlen($passwd) < 6 || strlen($passwd) > 20) {
            return $this->toJson(-2, '密码必须在6-20位以内');
        }
        $res = $this->obj()->dologin($uname, $passwd);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }


    //添加卡
    public function cardSet()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
        $pas['token'] = isset($param['token']) ? $param['token'] : '';
        $pas['serial'] = isset($param['serial']) ? $param['serial'] : ''; //隐秘ID
        $pas['serial'] = isset($param['serial']) ? $param['serial'] : ''; //隐秘ID
        $pas['cardno'] = isset($param['cardno']) ? $param['cardno'] : ''; //显现ID
        $pas['realname'] = isset($param['realname']) ? $param['realname'] : '';
        $pas['phone'] = isset($param['phone']) ? $param['phone'] : '';
        $pas['amount'] = isset($param['amount']) ? $param['amount'] : '';
        $res = $this->obj()->cardSet($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //卡充值
    public function recharge()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
        $serial = isset($param['serial']) ? $param['serial'] : ''; //隐秘ID
        $amount = isset($param['amount']) ? $param['amount'] : ''; //额度
        $amount *= 100;
        $res = $this->obj()->recharge($serial, $amount);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //卡列表
    public function cardList()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
        $token = isset($param['token']) ? $param['token'] : '';
        $status = isset($param['status']) ? $param['status'] : 0; //0所有，1使用中，2已退卡
        $res = $this->obj()->cardList($token, $status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //卡详情
    public function cardOne()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
        $serial = isset($param['serial']) ? $param['serial'] : '';
        $res = $this->obj()->cardOne($serial);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }


    //退卡
    public function cardRefund()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
        $serial = isset($param['serial']) ? $param['serial'] : '';
        $res = $this->obj()->cardRefund($serial);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //消费列表
    public function costList()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Credentials: true");
        $param = $this->request->param();
        $serial = isset($param['serial']) ? $param['serial'] : '';
        $type = isset($param['type']) ? $param['type'] : 0;
        if (!$serial) {
            return $this->toJson(-2, '卡ID必须的');
        }
        $res = $this->obj()->costList($serial, $type);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}